package com.budwk.app.sqfwV2.generator.dayingRoadExcel;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.bo.SqfwExcelForSummaryBO;
import com.budwk.app.sqfwDoc.bo.SqfwExcelJudgeRes;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseTemplateExcelGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.SqfwExcelCreateCommonBO;
import com.budwk.app.sqfwV2.dto.dayingRoad.DaYingLuRedRecordPdfDTO;
import com.budwk.app.sqfwV2.dto.dayingRoad.DaYingLuWhiteRecordPdfDTO;
import com.budwk.app.sqfwV2.dto.image.DaYingLuWhiteRecordExcelImageConfigBO;
import com.budwk.app.sqfwV2.enums.RedWhite;
import com.budwk.app.util.ClassUtil;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;

import java.util.*;

/**
 * excel大营路街道“治风”工作推进表.xlsx
 */
public class WhiteWorkPromotionExcelGenerator extends BaseTemplateExcelGenerator {

    @Override
    protected String getTemplateName() {
        return "daYingLu/white/workPromotion.xlsx";
    }

    /**
     * 可以参照 {@link com.budwk.app.sqfw.enums.DownLoadFileNameEnum}
     *
     * @return 文件名名称
     */
    @Override
    public String getDownloadFileName() {
        //TODO 在这里返回你生成的文件名名称
        // 名称要求带上随机数。如果仅仅是一个中文名称的话，上传到对象存储会造成文件覆盖
        // 注意注意注意不用带上后缀
        return "大营路街道“治风”工作推进表";
    }

    /**
     * 用户自定义excel基础参数，比如表头这样的公用数据
     *
     * @param excelParam       已经填充的基础数据据
     * @param excelCreateParam excel生成提供的原料
     */
    @Override
    protected void customerPopularSheetBaseParam(Map<String, Object> excelParam, SqfwExcelCreateCommonBO excelCreateParam) {
        // 是否只生成模板
        Boolean onlyGenerateTemplateFlag = excelCreateParam.getExcelCreateConfig().getOnlyGenerateTemplateFlag();
        if (onlyGenerateTemplateFlag) {
            for (String key : excelParam.keySet()) {
                excelParam.put(key, "");
            }
            return;
        }
        String district = excelCreateParam.getBaseExcelParamDTO().getDistrict();
        excelParam.put("district", "直属".equals(district) ? "" : district);
        String county = excelCreateParam.getBaseExcelParamDTO().getCounty();
        excelParam.put("county", "直属".equals(district) ? "" : county);
        excelParam.put("phone", excelCreateParam.getBaseExcelParamDTO().getCustomerParamMap().get("phone"));
    }

    @Override
    protected void customerPopularSheetDataParam(List<Map<String, Object>> listMap, SqfwExcelCreateCommonBO excelCreateParam) {

        // 是否只生成模板
        Boolean onlyGenerateTemplateFlag = excelCreateParam.getExcelCreateConfig().getOnlyGenerateTemplateFlag();
        if (onlyGenerateTemplateFlag) {
            List<Map> mapList = JSONUtil.toList(JSONUtil.toJsonStr(listMap), Map.class);
            for (Map<String, Object> map : mapList) {
                for (String key : map.keySet()) {
                    map.put(key, "");
                }
                listMap.clear();
                listMap.add(map);
                return;
            }
        }

        // 先把list清空，在重新统计
        listMap.clear();

        List<SqfwExcelForSummaryBO> dataList = excelCreateParam.getDataList();
        Map<String, Object> map = new HashMap<>();

        // 按社区进行统计

//        Set<String> set = new HashSet<>();
//        for (SqfwExcelForSummaryBO bo : dataList) {
//            set.add(bo.getSqfwFormSummaryNew().getCommunity());
//        }
//
//        for (String community : set) {
//            // 本周红事办理总户数
//            long redTotal = 0;
//            // 是否有高价彩礼
//            long isRedHighAmount = 0;
//            // 总桌数
//            long redTotalTable = 0;
//            // 总礼金数
//            long redAmount = 0;
//            // 宾客人数
//            long redGuestCount = 0;
//
//            // 本周白事办理总户数
//            long whiteTotal = 0;
//            // 总桌数
//            long whiteTotalTable = 0;
//            // 总礼金数
//            long whiteAmount= 0;
//            // 宾客人数
//            long whiteGuestCount = 0;
//            // 3天以内（户）
//            long ltThree = 0;
//            // 3天以上（户）
//            long gtThree = 0;
//            // 共计劝阻
//            long dissuadeTotal = 0;
//
//            for (SqfwExcelForSummaryBO bo : dataList) {
//                Sqfw_formSummary_new formSummary = bo.getSqfwFormSummaryNew();
//
//                if (!community.equals(formSummary.getCommunity())) continue;
//
//                JSONObject jsonObject = JSON.parseObject(formSummary.getParamData());
//
//                // 红事统计
//                if (RedWhite.RED.getName().equals(jsonObject.get(ClassUtil.getField(DaYingLuRedRecordPdfDTO::getCelebratoryEvents)))) {
//                    DaYingLuRedRecordPdfDTO dto = JSON.parseObject(formSummary.getParamData(), DaYingLuRedRecordPdfDTO.class);
//                    redTotal ++;
//                    // 高价彩礼
//                    Long dowryAmount = dto.getDowryAmount();
//                    if (dowryAmount / 100 > 10000) isRedHighAmount ++;
//                    redTotalTable += dto.getBanquetTableCount();
//                    redAmount += dto.getGiftAmount();
//                    redGuestCount += dto.getGuestCount();
//                }
//
//                // 白事统计
//                if (RedWhite.WHITE.getName().equals(jsonObject.get(ClassUtil.getField(DaYingLuRedRecordPdfDTO::getCelebratoryEvents)))) {
//                    DaYingLuWhiteRecordPdfDTO dto = JSON.parseObject(formSummary.getParamData(), DaYingLuWhiteRecordPdfDTO.class);
//                    whiteTotal ++;
//                    whiteTotalTable += dto.getBanquetTableCount();
//                    whiteAmount += dto.getGiftAmount();
//                    whiteGuestCount += dto.getGuestCount() == null ? 0 : dto.getGuestCount();
//                    if (dto.getFuneralDays() <= 3) {
//                        ltThree ++;
//                    } else {
//                        gtThree ++;
//                    }
//                    if (dto.getIsGuidanceDissuasionDone() != null && dto.getIsGuidanceDissuasionDone())
//                        dissuadeTotal ++;
//                }
//            }
//
//            map.put("community", community);
//
//            map.put("redTotal", redTotal);
//            map.put("isRedHighAmount", isRedHighAmount);
//            map.put("redAvgTable", redTotal > 0 ? redTotalTable / redTotal : 0);
//            map.put("redAvgAmount", redGuestCount > 0 ? (double)redAmount / redGuestCount / 100 : 0);
//
//            map.put("whiteTotal", whiteTotal);
//            map.put("ltThree", ltThree);
//            map.put("gtThree", gtThree);
//            map.put("whiteAvgTable", whiteTotal > 0 ? whiteTotalTable / whiteTotal : 0);
//            map.put("whiteAvgAmount", whiteGuestCount > 0 ? (double)whiteAmount / whiteGuestCount / 100 : 0);
//            map.put("dissuadeTotal", dissuadeTotal);
//
//            listMap.add(map);
//        }

        //按街道统计

        // 本周红事办理总户数
        long redTotal = 0;
        // 是否有高价彩礼
        long isRedHighAmount = 0;
        // 总桌数
        long redTotalTable = 0;
        // 总礼金数
        long redAmount = 0;
        // 宾客人数
        long redGuestCount = 0;

        // 本周白事办理总户数
        long whiteTotal = 0;
        // 总桌数
        long whiteTotalTable = 0;
        // 总礼金数
        long whiteAmount= 0;
        // 宾客人数
        long whiteGuestCount = 0;
        // 3天以内（户）
        long ltThree = 0;
        // 3天以上（户）
        long gtThree = 0;
        // 共计劝阻
        long dissuadeTotal = 0;

        for (SqfwExcelForSummaryBO bo : dataList) {
            Sqfw_formSummary_new formSummary = bo.getSqfwFormSummaryNew();

            JSONObject jsonObject = JSON.parseObject(formSummary.getParamData());

            // 红事统计
            if (RedWhite.RED.getName().equals(jsonObject.get(ClassUtil.getField(DaYingLuRedRecordPdfDTO::getCelebratoryEvents)))) {
                DaYingLuRedRecordPdfDTO dto = JSON.parseObject(formSummary.getParamData(), DaYingLuRedRecordPdfDTO.class);
                redTotal ++;
                // 高价彩礼
                Long dowryAmount = dto.getDowryAmount();
                if (dowryAmount / 100 > 10000) isRedHighAmount ++;
                redTotalTable += dto.getBanquetTableCount();
                redAmount += dto.getGiftAmount();
                redGuestCount += dto.getGuestCount();
            }

            // 白事统计
            if (RedWhite.WHITE.getName().equals(jsonObject.get(ClassUtil.getField(DaYingLuRedRecordPdfDTO::getCelebratoryEvents)))) {
                DaYingLuWhiteRecordPdfDTO dto = JSON.parseObject(formSummary.getParamData(), DaYingLuWhiteRecordPdfDTO.class);
                whiteTotal ++;
                whiteTotalTable += dto.getBanquetTableCount();
                whiteAmount += dto.getGiftAmount();
                whiteGuestCount += dto.getGuestCount() == null ? 0 : dto.getGuestCount();
                if ((dto.getFuneralDays() == null ? 0 : dto.getFuneralDays()) <= 3) {
                    ltThree ++;
                } else {
                    gtThree ++;
                }
                if (dto.getIsGuidanceDissuasionDone() != null && dto.getIsGuidanceDissuasionDone())
                    dissuadeTotal ++;
            }
        }

        map.put("redTotal", redTotal);
        map.put("isRedHighAmount", isRedHighAmount);
        map.put("redAvgTable", redTotal > 0 ? redTotalTable / redTotal : 0);
        map.put("redAvgAmount", redGuestCount > 0 ? (double)redAmount / redGuestCount / 100 : 0);

        map.put("whiteTotal", whiteTotal);
        map.put("ltThree", ltThree);
        map.put("gtThree", gtThree);
        map.put("whiteAvgTable", whiteTotal > 0 ? whiteTotalTable / whiteTotal : 0);
        map.put("whiteAvgAmount", whiteGuestCount > 0 ? (double)whiteAmount / whiteGuestCount / 100 : 0);
        map.put("dissuadeTotal", dissuadeTotal);

        listMap.add(map);
    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParam) {

    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, SqfwExcelForSummaryBO forSummaryBO) {

    }

    /**
     * 如果在遍历之前就能够判断excel是否生成，那么就在这个方法中进行判断
     *
     * @param excelCreateParam excel生成所需的最为全面的数据
     * @param excelParam       excel已经填充的部分数据
     */
    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeErgodic(SqfwExcelCreateCommonBO excelCreateParam, Map<String, Object> excelParam) {
        SqfwExcelJudgeRes sqfwExcelJudgeRes = new SqfwExcelJudgeRes();
        sqfwExcelJudgeRes.setFileIdentity(excelCreateParam.getExcelCreateConfig().getFileIdentity());
        if (excelCreateParam.getDataList() != null && excelCreateParam.getDataList().size() > 0) {
            sqfwExcelJudgeRes.setCreateConditionFlag(true);
        } else {
            sqfwExcelJudgeRes.setCreateConditionFlag(false);
        }
        return sqfwExcelJudgeRes;
    }

    /**
     * 如果在遍历之后能够判断excel是否生成，那么就在这个方法中进行判断
     *
     * @param excelParam excel已经填充的部分数据
     */
    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeCreate(Map<String, Object> excelParam) {
        return null;
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return DaYingLuWhiteRecordExcelImageConfigBO.class;
    }
}
